package com.xpgk.xpsoft.department;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.common.kit.CacheKitEx;
import com.xpgk.common.kit.PubKit;
import com.xpgk.xpsoft.common.XPSoftService;
import com.xpgk.xpsoft.constants.XPSoftConstant;

public class DepartmentService extends XPSoftService {
	
	static public String checkDeptRecord(Record dept) {
		if (dept == null)
			return "department is null";
		String value = dept.getStr("NAME");
		if (value == null)
			return "department name is null";
		value = dept.get("SYSTEMID");
		if (value == null || value.length() == 0)
			dept.set("systemID", XPSoftConstant.systemID);
		value = dept.getStr("BH");
		if (value == null)
			return "department bh is null";
		value = dept.getStr("STATE");
		if (value == null)
			dept.set("STATE", "0");

		return "ok";
	}

	// 添加用户
	static public synchronized String saveDept(Record dept) {
		String ret = checkDeptRecord(dept);
		if (!ret.equals("ok"))
			return ret;
		dept.remove("ID");
		if (ExistDeptBH(dept.getStr("BH"))) {
			dept.set("BH", GetNextDeptBH(dept.getInt("PARENTID")));
		}
		dept.set("CREATEDATE", new Date());
		dept.set("GXSJ", new Date());
		dept.set("SYSTEMID", 1);
		Db.save("FRM_DEPARTMENT", dept);
		CacheKitEx.remove(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_deptIDName);
		return "ok";
	}

	// 添加用户
	static public boolean ExistDeptBH(String deptBH) {
		return Db.findFirst(Db.getSql("xpSoftDepartment.ExistDeptBH"), deptBH) != null;
	}

	static public String GetNextDeptBH(int deptID) {
		Record record = Db.findFirst(Db.getSqlPara("xpSoftDepartment.findMaxDepartmentBH", Kv.by("ID", deptID)));
		if (record == null)
			return "0";
		String bh = record.getStr("BH");
		if (bh == null)
			bh = "0001";
		else {
			if (record.getInt("ID") == deptID)
				bh += "0001";
			else {
				int len = bh.length();
				if (len < 4)
					len = 4;
				String bb = bh.substring(len - 4, len);
				bb = "000" + String.valueOf(Integer.parseInt(bb) + 1);
				bb = bb.substring(bb.length() - 4);
				bh = bh.substring(0, len - 4) + bb;
			}
		}
		return bh;
	}

	// 更新部门信息
	static public String updateDept(Record dept) {
		if (dept == null || dept.get("id") == null)
			return "错误部门信息。";
		dept.set("GXSJ", new Date());
		if (Db.update("FRM_DEPARTMENT", dept)) {
			CacheKitEx.remove(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_deptIDName);
			return "ok";
		}
		return "错误部门信息。";
	}

	// 更新部门管理员权限
	static public String updateDeptRights(int deptID, Integer[] rights) {

		Db.update("delete from FRM_DEPARTMENT_RIGHT where DEPARTMENTID=?", deptID);
		if (rights == null)
			return "ok";
		List<Record> recordList = new ArrayList<Record>();
		for (int i = 0; i < rights.length; i++) {
			Record record = new Record();
			record.set("DEPARTMENTID", deptID);
			record.set("RIGHTID", rights[i]);
			record.set("JYW", HashKit.md5(deptID + "userRole" + rights[i]));
			recordList.add(record);
		}
		Db.batchSave("FRM_DEPARTMENT_RIGHT", recordList, 100);
		return "ok";
	}

	// 得到部门权限对应的树列表
	static List<Kv> getRightListForTree(int deptID) {

		Record record = Db.findById("FRM_DEPARTMENT", deptID);
		if (record == null)
			return null;
		List<Record> all = null;
		if (record.getInt("PARENTID") == -1)
			all = Db.find("select * from FRM_RIGHT where HIDDEN<2 and STATE=1 order by SORT");
		else
			all = Db.find(Db.getSqlPara("xpSoftDepartment.findDeptRightList", record.getInt("PARENTID")));
		List<Record> sub = Db.find(Db.getSqlPara("xpSoftDepartment.findDeptRightList", deptID));
		List<Kv> ret = new ArrayList<Kv>();
		ret.add(Kv.by("id", 0).set("name", "全部").set("open", true));
		int pID = 0;
		String preNode = "";
		for (Iterator<Record> iterator = all.iterator(); iterator.hasNext();) {
			Record r = (Record) iterator.next();
			Kv kv = Kv.by("id", r.getInt("id")).set("name", r.getStr("name")).set("open", false);
			String node = r.getStr("node");
			int id = r.getInt("id");
			if (node.length() == 2)
				kv.set("pId", 0);
			else {
				if (node.equals(preNode))
					kv.set("pId", pID);
				else {
					pID = id;
					preNode = node;
					node = node.substring(0, node.length() - 2);
					for (Iterator<Record> iterator2 = all.iterator(); iterator2.hasNext();) {
						Record r2 = (Record) iterator2.next();
						if (node.equals(r2.getStr("node"))) {
							kv.set("pId", r2.getInt("id"));
							break;
						}
					}
				}
			}
			for (Iterator<Record> iterator3 = sub.iterator(); iterator3.hasNext();) {
				Record r3 = (Record) iterator3.next();
				if (id == r3.getInt("id")) {
					kv.set("checked", true);
					break;
				}
			}
			ret.add(kv);
		}
		return ret;
	}

	// 得部门名称
	public static String getDeptName(int deptID) {
		Map<Integer, String> kv = CacheKitEx.get(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_deptIDName);
		if (kv == null) {
			List<Record> records = Db.find("select ID,NAME from FRM_DEPARTMENT");
			kv = PubKit.toMap(records, "ID", "NAME");
			CacheKitEx.put(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_deptIDName, kv);
		}
		return kv.get(deptID);
	}
}
